<?php
session_start();

$id = $_REQUEST["id"];

$db = NEW myDBs();

//echo "xxxx".$_SESSION["lvl1_id"];
if ($id) {
    $sql = "select * from tbl_pet where id = '$id'";

    $result = $db->runSQL($sql);
    $row = $db->fetch_object($result);

    $panngan2_id = $row->panngan2_id;
    $panngan3_id = $row->panngan3_id;

    $budget_year = $row->budget_year;

    $month_year = $row->month_year;

    $ex1_id = $row->ex1_id;
    $ex2_id = $row->ex2_id;
    $ex3_id = $row->ex3_id;

    $amount = $row->amount;
}
//$ex3_id = $_REQUEST["ex3_id"];
?>

<script type="text/javascript">
    $(document).ready(function() {
        
        $("#receiver").select2({
            placeholder: "เลือก..."
            , allowClear: true
            //, maximumSelectionSize: 2
                    //, tags:["red", "green", "blue"]
        });
        
        $("#lvl1_id").select2({
            placeholder: "เลือก..."
        });
        
        $("#panngan2_id").chained("#lvl1_id");
        $("#panngan3_id").chained("#panngan2_id");

        $.post('dropdown_1.php', {tablename: 'expenses_lvl1', dd: 'ex1_id', idname: 'expenses1_id', fieldname: 'expenses1_name', by: '', ex1_id: '<?php echo $ex1_id; ?>', ex2_id: '<?php echo $ex2_id; ?>', ex3_id: '<?php echo $ex3_id; ?>'}, function(data) {
            $("#expenses1").html(data);
        });

        $("input,select,textarea").not("[type=submit]").jqBootstrapValidation({
            submitSuccess: function($form, event) {
                event.preventDefault();

                $.ajax({
                    type: "POST",
                    url: "pet_process.php",
                    data: $('#pet').serialize(),
                    cach: false,
                    error: function() {
                        alert("ERROR !!!!");
                    },
                    success: function(data) {

//                        $("#test").html(data);
                        window.location.href = "index.php?pages=pet&pageid=9";

                    }
                });
            }
        });

        $("#panngan2_id").change(function() {
//            var panngan2 = $(this).val();
//            var panngan3 = "<?php // echo $panngan3_id; ?>";
//
//            //alert(panngan2);
//
//            if (panngan2 != "") {
//                //alert(panngan2);
//                $.post('dd_panngan.php', {panngan2: panngan2, panngan3: panngan3}, function(data) {
//                    $("#dd_panngan3").html(data);
//
//                });
//            }

        });

        $("#month_year").datepicker({
            dateFormat: "yy-mm"
                    //defaultDate: new Date()
                    //showMonthAfterYear:true,

        });

        $("#date_create").datepicker({
            dateFormat: "yy-mm-dd"
                    //defaultDate: new Date()
                    //showMonthAfterYear:true,

        });
//        $('#panngan2_id').trigger('change');

        $("#include").click(function() {

            if ($(this).is(":checked")) {
                //alert("check");


                $("#amount").removeAttr("readonly");

                $("#amount1").attr("readonly", true);
                $("#amount1").val("");
                $("#amount").val("");

                $.fn.myCalc();
            } else {
                //alert("uncheck");
                $("#amount").attr("readonly", true);
                $("#amount").val("");
                $("#amount1").val("");

                $("#amount1").removeAttr("readonly");

                $.fn.myCalc();
            }

        });

        $.fn.myCalc = function() {
            var amount = $('#amount').val();
            var amount1 = $('#amount1').val();
//            var tax = $('#tax').val();
            var tax1 = $("#tax1").val();
            var tax = $("#tax").val();
            var penalty = $("#penalty").val();

            if (amount == "")
                amount = 0.00;

            if (amount1 == "")
                amount1 = 0.00;

//            if (amount >= 10000)
//                amount = 0.00;
//            if (tax == "") tax = 0.00;
            if (tax1 == "")
                tax1 = 0.00;
            if (penalty == "")
                penalty = 0.00;

            var net = 0.00;

            amount = parseFloat(amount);
            tax1 = parseFloat(tax1);
            tax = parseFloat(tax);
            penalty = parseFloat(penalty);

            
            
            if ($("#include").is(":checked")) {
                tax = amount * tax / 100;
                var a1 = 0.00;
                a1 = amount + tax;
                $("#amount1").val(a1.toFixed(2));
                
                tax1 = amount * tax1 / 100;
                
                net = a1 - tax1 - penalty;
                $("#net").val(net.toFixed(2));
            } else {
//                tax = amount * tax / 100;
                var a = 0.00;
                tax = amount1 - ((amount1 * 100) / (100 + tax));

                a = amount1 - tax;
                $("#amount").val(a.toFixed(2));
                
                tax1 = a * tax1 / 100;
                
                net = amount1 - tax1 - penalty;
                $("#net").val(net.toFixed(2));
            }


            $("#t1").html(tax1.toFixed(2));
            $("#tt1").val(tax1.toFixed(2));

            $("#t").html(tax.toFixed(2));
            $("#tt").val(tax.toFixed(2));

//            net = amount - tax1 - penalty;
//            $("#net").val(net.toFixed(2));

            //amount.
//            $("#s").html(amount.toFixed(2));
        }

        $('#amount').number(true, 2);
        $('#amount1').number(true, 2);
        $('#tax').number(true, 2);
        $('#tax1').number(true, 2);
        $('#penalty').number(true, 2);

        $("#amount").keyup(function() {
            $.fn.myCalc();
        });

        $("#amount1").keyup(function() {
            $.fn.myCalc();
        });
        $("#penalty").keyup(function() {
            $.fn.myCalc();
        });
        $("#tax1").keyup(function() {
            $.fn.myCalc();
        });

        $("#tax").keyup(function() {
            $.fn.myCalc();
//            var amount = $("#amount").val();
//            var tax = $("#tax").val();
//
//            amount = parseFloat(amount);
//            tax = parseFloat(tax);
//
//            tax = amount - ((amount * 100) / (100 + tax));
//
//            $("#t").html(tax.toFixed(2));
//            $("#tt").val(tax.toFixed(2));

        });

    }); //(document).ready


</script>
<div id="test"></div>
<form id="pet" name="pet" method="post" action="index.php" novalidate="">
    <input type="hidden" name="id" value="<?php echo $id; ?>">
    <input type="hidden" name="task" value="<?php echo ($id) ? "edit" : "new"; ?>">

    <div class="row">
        <div class="span7">
            <div class="panel panel-info">
                <div class="panel-heading"></div>
                <div class="panel-body">
                    <div class='control-group'>
                        <label class="control-label" for="ปีงบประมาณ">ปีงบประมาณ</label>
                        <div class="controls ">
                            <select id="budget_year" name="budget_year" <?php echo ($id) ? "readonly" : ""; ?>>

                                <?php
                                $this_y = date("Y");

                                if ($this_y < 2556)
                                    $this_y += 543;

                                if (!$budget_year)
                                    $budget_year = $this_y;

                                for ($i = 2556; $i <= $this_y + 1; $i++) {
                                    if ($i == $budget_year) {
                                        $selected = "selected";
                                    } else {
                                        $selected = "";
                                    }
                                    echo "<option value='$i' $selected>" . $i . "</option>";
                                }
                                ?>
                            </select>
                        </div>
                    </div>

                    <div class='control-group'>
                        <label class="control-label" for="สำกัดกอง">สำกัดกอง</label>
                        <div class="controls ">
                            <select id="lvl1_id" name="lvl1_id" style="width: 350px;">

                                <option value="">--</option>
                                <?php
                                $sql = "SELECT * "
                                        . " FROM v_lvl1 ";

                                $result = $db->runSQL($sql);

                                while ($rows = $db->fetch_object($result)) {
                                    if ($rows->lvl1_id == $lvl1_id)
                                        $selected = "selected";
                                    echo "<option value='$rows->lvl1_id' $selected>$rows->lvl1_name</option>";
                                    $selected = "";
                                }
                                ?>

                            </select>
                        </div>
                    </div>
                    
                    
                    <div class='control-group'>
                        <label class="control-label" for="แผนงาน">แผนงาน</label>
                        <div class="controls ">
                            <select id="panngan2_id" name="panngan2_id" class="span5" >

                                <option value="">--</option>
                                <?php
                                $sql = "SELECT DISTINCT SUBSTR(lvl_id,1,2) AS lvl_id, panngan2_id, panngan2_name "
                                        . " FROM v_panngan2 "
                                        . " ";

                                $result = $db->runSQL($sql);

                                while ($rows = $db->fetch_object($result)) {
                                    if ($rows->panngan2_id == $panngan2_id)
                                        $selected = "selected";
                                    echo "<option class='$rows->lvl_id' value='$rows->panngan2_id' $selected>$rows->panngan2_name</option>";
                                    $selected = "";
                                }
                                ?>

                            </select>
                        </div>
                    </div>
                    
                    <div class='control-group'>
                        <label class="control-label" for="งาน">งาน</label>
                        <div class="controls ">
                            <select id="panngan3_id" name="panngan3_id" class="span5" >

                                <option value="">--</option>
                                <?php
                                $sql = "SELECT DISTINCT * "
                                        . " FROM v_panngan3 "
                                        . " ";

                                $result = $db->runSQL($sql);

                                while ($rows = $db->fetch_object($result)) {
                                    if ($rows->panngan3_id == $panngan3_id)
                                        $selected = "selected";
                                    echo "<option class='$rows->panngan2_id' value='$rows->panngan3_id' $selected>$rows->panngan3_name</option>";
                                    $selected = "";
                                }
                                ?>

                            </select>
                        </div>
                    </div>
                    
<!--                    <div class="control-group">
                        <label class="control-label" for="แผนงาน">แผนงาน</label>
                        <div class="controls">
                            <select class="span5" id="panngan2_id" name="panngan2_id" required>
                                <option value="">เลือก...</option>
                                <?php
                                //$db = NEW myDBS;
//                                $db->selectDB("hr_udoncity");
////WHERE lvl_id like '" . $_SESSION["lvl1_id"] . "%'
//                                $sql = "SELECT DISTINCT panngan2_id 
//                                        FROM position 
//                                        WHERE lvl_id like '%%'
//                                        AND panngan2_id IS NOT NULL";
//
//                                $result = $db->runSQL($sql);
//
//                                while ($rows = $db->fetch_object($result)) {
//                                    $db->selectDB("account");
//                                    $sql = "SELECT DISTINCT * FROM panngan2 WHERE panngan2_id = '" . $rows->panngan2_id . "'";
//                                    $result1 = $db->runSQL($sql);
//                                    while ($rows1 = $db->fetch_object($result1)) {
//                                        if ($rows1->panngan2_id == $panngan2_id) {
//                                            $selected = " selected";
//                                        } else {
//                                            $selected = "";
//                                        }
//                                        echo "<option value='" . $rows1->panngan2_id . "' $selected>$rows1->panngan2_name</option>";
//                                    }
//                                }
                                ?>
                            </select>
                        </div>
                    </div>

                    <div class="control-group">
                        <label class="control-label" for="งาน">งาน</label>
                        <div class="controls " id="dd_panngan3">
                            <select class="span5" id="panngan3_id" name="panngan3_id">
                                <option value="">เลือก</option>

                            </select>
                        </div>
                    </div>/span-->

                    <div class="control-group">
                        <label class="control-label" for="หมวด / ลักษณะ">หมวด / ลักษณะ</label>
                        <div id="expenses1" class="controls">
                            <select class="span5" id="ex1_id" name="ex1_id" required>
                                <option value="">เลือก...</option>
                            </select>
                        </div>
                    </div>

                    <div class="control-group">
                        <label class="control-label" for="ประเภท">ประเภท</label>
                        <div id="expenses2" class="controls">
                            <select  class="span4" id="ex2_id" name="ex2_id" required>
                                <option value="">เลือก...</option>
                            </select>
                        </div>
                    </div>


                    <div class="control-group">
                        <label class="control-label" for="รายการ">รายการ</label>
                        <div id="expenses3" class="controls">
                            <select class="span5" id="ex3_id" name="ex3_id">
                                <option value="">เลือก...</option>
                            </select>
                        </div>
                    </div>
                    
                    <div class="control-group">
                        <label class="control-label" for="ชื่อโครงการ">ชื่อโครงการ</label>
                        <div class="controls">
                            <input type="text" class="input-xxlarge" id="proj_name" name="proj_name" value="<?php echo $proj_name; ?>" />
                        </div>
                    </div>

                    <div class="control-group">
                        <label class="control-label" for="วันที่">วันที่</label>
                        <div class="controls">
                            <input type="text" id="date_create" name="date_create" value="<?php echo $row->date_create; ?>" required=""/>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="เดือน/ปี">เดือน/ปี</label>
                        <div class="controls">
                            <input type="text" id="month_year" name="month_year" value="<?php echo $month_year; ?>" />
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="ผู้รับเงิน">ผู้รับเงิน</label>
                        <div class="controls">
                            <!--<input type="text" id="receiver" name="receiver" value="<?php // echo $row->receiver; ?>" />-->
                            <select  id="receiver" name="receiver" style="width: 250px;">
                                <option value="" selected>--</option>
                                <?php
                                //$sql = "select * from tbl_creditor ";
                                $sql = "select * from v_vender";

                                $result = $db->runSQL($sql);
                                while ($row2 = mysql_fetch_object($result)) {
                                    ?>
                                    <option value="<?php echo $row2->vender_id; ?>" <?php echo ($row->receiver == $row2->vender_id) ? " selected" : ""; ?>><?php echo $row2->vender_name; ?></option>
                                    <?php
                                }
                                ?>
                            </select>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="หมายเหตุ">หมายเหตุ</label>
                        <div class="controls">
                            <textarea id="comment" name="comment" rows="7" style="width: 350px" placeholder="เบิกจ่ายให้แก่... เพื่อ....."><?php
                                if ($row->comment) {
                                    echo $row->comment;
                                } else {
                                    //echo "เบิกจ่ายให้แก่... \nเพื่อ.....";
                                }
                                ?></textarea>

<!--<input type="text" id="comment" name="comment" value="<?php echo $row->comment; ?>" />-->
                        </div>
                    </div>

                </div>
            </div>
        </div>
        <div class="span5">
            <div class="panel panel-info">
                <div class="panel-heading"></div>
                <div class="panel-body">

                    <div class="control-group">
                        <label class="control-label" for="มูลค่าสินค้า / บริการ">มูลค่าสินค้า / บริการ</label>
                        <div class="controls">
                            <input type="text" class="text-right" id="amount" name="amount" value="<?php echo $amount; ?>" required placeholder="0.00"/>&nbsp;บาท
                            <!--<input type="text" class="text-right" id="" name="" placeholder="0.00">-->
                        </div>
                    </div>

                    <div class="control-group">
                        <label class="control-label" for="ภาษี มูลค่าเพิ่ม">ภาษี มูลค่าเพิ่ม (%)</label>
                        <div class="controls">
                            <input type="text" class="text-right span1" id="tax" name="tax" value="<?php echo ($row->tax) ? $row->tax : "7"; ?>" placeholder="0">&nbsp;&nbsp;<span class="text-right uneditable-input span2" id="t">&nbsp;<?php echo $row->tt; ?></span>
                            <input type="hidden" id="tt" name="tt" value="<?php echo $row->tt; ?>"><br>
                            <label class="checkbox">
                                <input type="checkbox" id="include" name="include" checked="">รวมภาษีมูลค่าเพิ่ม
                            </label>
                        </div>
                    </div>

                    <div class="control-group">
                        <label class="control-label" for="จำนวนเงิน">จำนวนเงิน</label>
                        <div class="controls">
                            <!--<span class="uneditable-input text-right" id="s"><?php // echo ($amount) ? $amount : "0.00";   ?></span>&nbsp;บาท-->
                            <input type="text" class="text-right" id="amount1" name="amount1" value="<?php echo ($row->amount1)? $row->amount1 : ""; ?>" placeholder="0.00" readonly="">&nbsp;บาท
                        </div>
                    </div>

                    <div class="control-group">
                        <label class="control-label" for="หักภาษี ณ ที่จ่าย">หักภาษี ณ ที่จ่าย</label>
                        <div class="controls">
                            <input type="text" class="text-right span1" id='tax1' name="tax1" value="<?php echo $row->tax1; ?>" placeholder="0">&nbsp;&nbsp;<span class="text-right uneditable-input span2" id="t1">&nbsp;<?php echo $row->tt1; ?></span>
                            <input type="hidden" id="tt1" name="tt1" value="<?php echo $row->tt1; ?>">
                        </div>
                    </div>

                    <div class="control-group">
                        <label class="control-label" for="ค่าปรับ">ค่าปรับ</label>
                        <div class="controls">
                            <input type="text" class="text-right" id='penalty' name="penalty" value="<?php echo $row->penalty; ?>" placeholder="0.00">&nbsp;บาท
                        </div>
                    </div>

                    <div class="control-group">
                        <label class="control-label" for="">จำนวนเงินสุทธิ</label>
                        <div class="controls">
                            <input type="text" class="text-right" id='net' name="net" value="<?php echo $row->net; ?>" readonly="">&nbsp;บาท
                        </div>
                    </div>

                    <!--                    </p>-->
                </div>

            </div>
        </div>
        <div class="row">
            <p class="text-center">
                <button type="submit" id="submit" name="submit"  class="btn btn-primary"><i class="icon-ok icon-white"></i> บันทึกข้อมูล</button>
                <a href="index.php?pages=pet" class="btn close-dialog"><i class="icon-remove"></i> ยกเลิก</a>
            </p>
        </div>

    </div>

</form> 
